/***
 *   程序的首要职责
 *         管理复杂度
 *
 *
 *   不要重复制造轮子
 * **/

let tableData;

$(document).ready(function () {

  /*查询部分的页面初始化*/
  initSelect('http://test.org/dept/list','#sltDept','----所有部门---',true);
  initSelect('http://test.org/title/list','#sltTitle','----所有职务---',true);
  initSelect('http://test.org/super/list','#sltSuper','----所有上级---',true);

  /*添加/修改窗体的页面初始化*/
  initSelect('http://test.org/dept/list','#sltDeptForInfo');
  initSelect('http://test.org/title/list','#sltTitleForInfo');
  initSelect('http://test.org/super/list','#sltSuperForInfo');

  initTable();

 /* $('#btnQuery').click(function () {
      //dosth
      //.....
  })
*/
  $('#btnQuery').bind('click',btnQueryClick);
  $('#btnEnter').bind('click',btnEnterClick)
  $('#btnSubmit').bind('click',btnSubmitClick);
  $('#btnChange').bind('click',btnChangeClick)
  $('#btnOut').bind('click',btnOutClick);

})


function btnOutClick(){

  if(confirm("是否办理当前员工的离职手续")){
    let row = $('#list').bootstrapTable('getSelections')[0];
    tableData[row.id-1].status ='out';
    initTable();
  }
}

function btnChangeClick(){

  $('#infoTitle').text('员工转职');

   let row = $('#list').bootstrapTable('getSelections')[0];
   console.log(row);

  $('#empId').val(row.id);
  $('#empNameForInfo').val(row.name);
  $('#sltDeptForInfo').val(row.dept.id);
  $('#sltTitleForInfo').val(row.title.id);
  $('#sltSuperForInfo').val(row.super.id);

  $('#info').modal();


}


function btnSubmitClick(){

  // $.ajax({
  //    url:'/add',
  //    data:{
  //
  //    },success:function (data) {
  //        initTable();
  //   }
  // })

  let empId = $('#empId').val();
  let dept = depts.find(function (item) {
    return item.id == $('#sltDeptForInfo').val();
  })

  if(empId==''){
    tableData.unshift({
      id:10,
      name:$('#empNameForInfo').val(),
      dept:dept,
      status:'in'
    });
  }else {
    tableData[empId-1] = {
         id:empId,
         name:$('#empNameForInfo').val(),
         dept:dept
        /*自行补全职务和上级相关内容*/
    }
  }

  $('#info').modal('hide');

  initTable();

}

function  btnEnterClick(){
  $('#infoTitle').text('员工入职');
  $('#info').modal();
}

function btnQueryClick() {

 /* if ($('#empName').val().trim()!='') {
    tableData=tableData.filter(function (emp) {
      return emp.name == $('#empName').val().trim();
    })
  }*/
  if($('#sltDept').val()!=0){
    tableData=tableData.filter(function (emp) {
      return emp.dept.id == $('#sltDept').val();
    })
  }


   initTable();

}


function initSelect(url,selector,defaultText,isShowAll) {

  $.ajax({
     url:url,//
     method:'get',
    dataType:'json',
    success:function (data) {

       if(isShowAll){
         data.unshift({
           id:0,
           name:defaultText
         })
       }


       $.each(data,function (index,item) {
           let opt = $('<option></option>').val(item.id).text(item.name);
           $(selector).append(opt);
       })

    }
  })

}

function initTable() {
  $('#list').bootstrapTable('destroy');
  if (!tableData) {
    $.ajax(
      {
        url: 'http://test.org/employee/list',
        method: 'get',
        dataType: 'json',
        success: function (data) {

          data = data.filter(function (item) {
             return item.status!='out';
          })

          $('#list').bootstrapTable({
            data: data,
            pagination: true,
            classes: ["table", "table-bordered", "table-striped", "table-hover"],
            clickToSelect: true,
            singleSelect: true,
            toolbar: '#toolbar',
            columns: columns
          })

          tableData = data;
        }
      }
    )
  } else {

    tableData = tableData.filter(function (item) {
      return item.status!='out';
    })
    $('#list').bootstrapTable({
      data: tableData,
      pagination: true,
      classes: ["table", "table-bordered", "table-striped", "table-hover"],
      clickToSelect: true,
      singleSelect: true,
      toolbar: '#toolbar',
      columns: columns
    })
  }
}


let columns = [
  {
    checkbox: true
  },
  {
    field: 'name',
    title: '姓名'
  }, {
    field: 'dept.name',
    title: '部门'
  }, {
    field: 'title.name',
    title: '职务'
  }, {
    field: 'super.name',
    title: '上级'
  }, {
    field: "status",
    title: "在职状态",
    formatter: function (val, row, index) {
      return val == 'in' ? '在职' : '离职';
    }
  }
]
